﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Model.BasicEventInfo;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.DD.Controllers
{
    public class TaskController : Controller
    {
        //
        // GET: /DD/Task/

        public ActionResult TaskList()
        {
            this.ViewData["start"] = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            this.ViewData["end"] = DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss");
            return View();
        }

        //查询
        public ActionResult DataLoad()
        {
            string driver = "";
            string doctor = "";
            string nurse = "";
            string stretcher = "";
            string actualNumber = "";
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //获取查询条件
            DateTime start = Convert.ToDateTime(Request.Form["start"]);
            DateTime end = Convert.ToDateTime(Request.Form["end"]);          
            string center = Request.Form["center"];
            string station = Request.Form["station"];
            string localAddress = Request.Form["localAdr"];
            string patientName = Request.Form["pName"];
            string sendAddress = Request.Form["sendAdr"];
            if (Request.Form["actualNumber"] == null)
            {
                actualNumber = "";
            }
            else
            {
                actualNumber = Request.Form["actualNumber"];
            }
            if (Request.Form["driver"] == null)
            {
                driver = "";
            }
            else
            {
                driver = Request.Form["driver"];
            }
            if (Request.Form["doctor"] == null)
            {
                doctor = "";
            }
            else
            {
                doctor = Request.Form["doctor"];
            }
            if (Request.Form["nurse"] == null)
            {
                nurse = "";
            }
            else
            {
                nurse = Request.Form["nurse"];
            }
            if (Request.Form["stretcher"] == null)
            {
                stretcher = "";
            }
            else
            {
                stretcher = Request.Form["stretcher"];
            }
            string callNumber = Request.Form["callNumber"];
            string contactTelephone = Request.Form["contactTelephone"];
            string ambulanceNumber = Request.Form["ambulanceNumber"];
                      
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            string eventDispatchCenterID = UserOperateContext.Current.Seesion_Dispatch_SubCenterID; //2020-11-22 zch 登录人所处急救中心
            TaskBLL bll = new TaskBLL();
            var list = bll.GetTaskList(pageSize, pageIndex, start, end, center, station, actualNumber, ambulanceNumber, driver,
                                       doctor,nurse, stretcher, patientName, callNumber, contactTelephone, localAddress, sendAddress,
                                       em, selfWorkCode, selfCenterID, selfStationID, eventDispatchCenterID);
            JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
            return j;

        }
        /// <summary>
        /// 修改任务信息
        /// </summary>
        [AllowAnonymous]
        [HttpPost]
        public ActionResult UpdateTask(TaskAmbulanceInfo binfo)
        {
            if (string.IsNullOrEmpty(binfo.ResultReason))
                binfo.ResultReason ="-1";
            P_User pUser = new P_User();
            pUser = UserOperateContext.Current.Session_UsrInfo;//获取登录人信息
            TaskBLL m_BLL = new TaskBLL();
            PersonInfo p = m_BLL.GetPreson(pUser.WorkCode);
            if(p!=null)
                ViewData["AgentCode"] = p.Code;
            else
                ViewData["AgentCode"] = pUser.ID;
            bool bsave = m_BLL.UpdateTask(binfo, ViewData["AgentCode"].ToString());
            if (bsave)
            {
                return Json(new { IsSuccess = true, Message = "修改成功" }, "text/html", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { IsSuccess = false, Message = "修改失败" }, "text/html", JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 获取修改痕迹
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public ActionResult GetCrackInfos(string TaskCode)
        {
            TaskBLL m_BLL = new TaskBLL();
            int pageSize = 15;
            if (Request.Form["rows"] != null)
            {
                pageSize = int.Parse(Request.Form["rows"]);
            }
            //获取请求的页码
            int pageIndex = 1;
            if (Request.Form["page"] != null)
            {
                pageIndex = int.Parse(Request.Form["page"]);
            }
            int rowCount = 0;
            List<ModifyRecordInfo> list = m_BLL.GetCrackInfos(pageIndex, pageSize, ref rowCount, TaskCode);
            return Json(new { total = rowCount, rows = list }, "text/html", JsonRequestBehavior.AllowGet);
        }
    }
}
